package com.jingdianjichi.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.application.Dto.SubjectInfoDTO;
import com.jingdianjichi.application.convent.SubjectAnswerConventer;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectInfoController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.add.dto,{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkArgument(!StringUtils.isEmpty(subjectInfoDTO.getSubjectDifficult()), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryIds(), "分类ID不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelIds(), "标签ID不能为空");


            SubjectInfoBo subjectInfoBo = new SubjectInfoBo();
            BeanUtils.copyProperties(subjectInfoDTO, subjectInfoBo);
            List<SubjectAnswerBo> subjectAnswerBos = SubjectAnswerConventer.INSERT.conventLIstToBO(subjectInfoDTO.getOptionList());

            subjectInfoBo.setOptionList(subjectAnswerBos);
            subjectInfoDomainService.add(subjectInfoBo);


            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectInfoController.add.error,{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }
    }


    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.getSubjectPage.dto,{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkArgument(!StringUtils.isEmpty(subjectInfoDTO.getSubjectDifficult()), "题目难度不能为空");

            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类ID不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签ID不能为空");


            SubjectInfoBo subjectInfoBo = new SubjectInfoBo();
            BeanUtils.copyProperties(subjectInfoDTO, subjectInfoBo);


            PageResult<SubjectInfoBo> subjectInfoBoPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBo);

            PageResult<SubjectInfoDTO> subjectInfoDTOPageResult = new PageResult<>();
            BeanUtils.copyProperties(subjectInfoBoPageResult, subjectInfoDTOPageResult);

            return Result.ok(subjectInfoDTOPageResult);
        } catch (Exception e) {
            log.error("SubjectInfoController.getSubjectPage.error,{}", e.getMessage(), e);
            return Result.fail("查询题目失败");
        }
    }


    @PostMapping("/querySubjectInfo")
    public Result<PageResult<SubjectInfoDTO>> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.querySubjectInfo.dto,{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目ID不能为空");
            SubjectInfoBo subjectInfoBo = new SubjectInfoBo();
            BeanUtils.copyProperties(subjectInfoDTO, subjectInfoBo);


            PageResult<SubjectInfoBo> subjectInfoBoPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBo);
            PageResult<SubjectInfoDTO> subjectInfoDTOPageResult = new PageResult<>();
            BeanUtils.copyProperties(subjectInfoBoPageResult, subjectInfoDTOPageResult);

            return Result.ok(subjectInfoDTOPageResult);
        } catch (Exception e) {
            log.error("SubjectInfoController.querySubjectInfo.error,{}", e.getMessage(), e);
            return Result.fail("查询题目失败");
        }
    }


}
